//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.carhailing.system.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.security.MD5Encoder;

public class MD5Sign {
    public MD5Sign() {
    }

    public static void main(String[] args) {
        Map<String, String> map = new HashMap();
        map.put("mobile", "15932412412");
        map.put("content", "发送验证码");
        map.put("noceStr", UUID.randomUUID().toString());
        map.put("timestamp", String.valueOf(System.currentTimeMillis()));
        String appSercret = "aea43111aa87807fb79939b28256b929";
        String sign = sign(map, appSercret);
        System.out.println("签名为=================" + sign);
    }

    public static String md5Encode(String text) {
        String str = null;

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            str = MD5Encoder.encode(md.digest(text.getBytes()));
        } catch (NoSuchAlgorithmException var3) {
            var3.printStackTrace();
        }

        return str;
    }

    public static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map != null && !map.isEmpty()) {
            Map<String, String> sortMap = new TreeMap();
            Iterator<String> iter = map.keySet().iterator();

            while(iter.hasNext()) {
                String key = (String)iter.next();
                String value = (String)map.get(key);
                if (!"merId".equals(key) && !"sign".equals(key) && value != null && !value.equals("")) {
                    sortMap.put(key, value);
                }
            }

            return sortMap;
        } else {
            return null;
        }
    }

    public static String sign(Map<String, String> map, String appSercret) {
        System.out.println("排序前：" + map);
        map = sortMapByKey(map);
        System.out.println("排序后" + map);
        List<Object> list = new ArrayList();
        String str = "";
        Iterator<String> iter = map.keySet().iterator();

        while(iter.hasNext()) {
            String key = (String)iter.next();
            Object value = map.get(key);
            list.add(key + "=" + value);
        }

        str = StringUtils.join(list, "&") + "&" + appSercret;
        System.out.println(str);
        return md5Encode(str);
    }
}
